<template>
	<view style="padding: 20rpx">
		<view class="flex-col justify-center ">
			<!-- 			<view class="flex-col justify-center "
				style="background-color: #f3f3f3;font-size: 28rpx; border-radius: padding: 5px; 5px;margin: 5px; line-height: 20px;">
				<text style="text-indent:26px">为了您的健康咨询更有效，请参照以下内容提供，以便专家正确评估您的身体状况，提出合计建议。</text>
				<text style="text-indent:26px"> 您可以提供体检报告、舌头近照等图片。您提供的咨询是一对一专家，我们会保护您的隐私。</text>
				<text style="text-indent:26px">
					您提供的咨询我们无法做到实时反馈，如有紧急情况请到当地健康机构咨询。
				</text>
				<text style="text-indent:26px">
					我们竭诚为您服务，提供精心的健康咨询。让我们共同打造一个有特色的健康管理平台！
				</text>
			</view> -->

			<view class="flex-row items-center  justify-center">

				<view class="flex-1">
					<view style="padding 20px 10px; font-size: 14px; color:#ff7700;margin: 10px 0px ">
						请在此输入您要咨询的内容
					</view>
					<u-textarea customStyle="background-color: #f2f2f2" :height="350" :maxlength="-1" v-model="content"
						placeholder="请输入咨询内容" @confirm="save()"></u-textarea>
				</view>


			</view>
			<view style="padding 20px 10px; font-size: 14px; color:#ff7700;margin: 10px 0px ">
				请您上传健康报告、舌头近照
			</view>
			<u-upload :fileList="fileList6" @afterRead="afterRead" @delete="deletePic" name="6" multiple :maxCount="6"
				width="100px" height="100px">
				<view class="flex-col justify-center items-center">
					<view style="width: 100px; height: 100px;background-color: #fdfdfd;"
						class="flex-col justify-center items-center">
						<image :src="fileName?fileName:`../../static/image/pic.png`" mode="aspectFill"
							style="width: 100rpx;height: 100rpx;">
						</image>
						<view style="color: #888; padding: 10px;" v-if="!userInfo.user.meta.qrCode">在此上传
						</view>
					</view>
				</view>


			</u-upload>
			<u-gap size="10"></u-gap>
			<view style="text-align: center;">
				点击阅读<text @click="goHelper" style="color: #0f8bda;">《健康咨询使用指南》</text>
			</view>
			<u-gap size="10"></u-gap>
			<button :disabled="submited" type="primary" @click="save" size="mini"
				style="margin: 0 20rpx; font-size: 32rpx;">提交</button>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				protocol: {},
				content: "",
				fileList6: [],
				fileName: "",
				submited: false,
			}
		},
		onLoad() {
			this.getContract();
		},
		methods: {
			goHelper() {
				uni.navigateTo({
					url: '/pages-publics/healthHelper/healthHelper'
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'success',
						message: '上传中'
					})
				});

			},

			formatDate(date) {
				return this.$Utils.formatDate(date, 'yyyy-MM-dd HH:mm:ss')
			},
			save() {
				if (this.submited) {
					return;
				}

				if (!this.content) {
					uni.showToast({
						title: '请输入咨询的内容',
						icon: 'none'
					});
					this.submited = false;
					return;
				}
				this.submited = true;
				let postData = {
					content: this.content
				}
				if (this.fileList6.length > 0) {
					this.$get({
						url: `${this.$Utils.ApiUrl}/files/signature`,
						data: {
							dir: 'images/'
						}
					}).then(async (aRes) => {
						let i = 0;
						for (let file of this.fileList6) {
							const result = await this.$Utils.uploadFilePromise(file.url, aRes);
							this.$set(postData, 'imageUrl' + i, result)
							i++;
						}

						this.doPost(postData);

					})
				} else {
					this.doPost(postData);
				}

			},
			doPost(postData) {
				this.$post({
					url: `${this.$Utils.ApiUrl}/healthConsult/joyMessages`,
					data: postData
				}).then((res) => {
					this.content = "";

					this.fileList6 = [];
					this.submited = false;
					this.$refs.utoast.show({
						message: "提交成功",
						complete() {
							uni.navigateBack()
						}
					})
				})
			},
			getContract() {
				this.$get({
					url: `${this.$Utils.ApiUrl}/protocols/health_consult_instruction/latest`,
					data: {

					}
				}).then(res => {
					this.protocol = res;
					this.content = this.protocol.content;
					console.log(res)
				})

			},
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>